Plugin support for revision weblink Support for an extension point that allows for weblinks to several external tools. First effort, weblinks for patch sets. At the present time GitWebLink is integrated. But the aim is to extend support to engulf entire GitWebLink and provide backwards support by adding a plugin that has the same functionality as GitWebLink has today. Change-Id: I19f4d00325d2991df9514ee8833a8873649c27bc 
diff --git a/Documentation/dev-plugins.txt b/Documentation/dev-plugins.txt index 575f5fa..dc98e11 100644 --- a/Documentation/dev-plugins.txt +++ b/Documentation/dev-plugins.txt 
@@ -1706,6 +1706,39 @@  The download schemes and download commands which are used most often  are provided by the Gerrit core plugin `download-commands`.   +[[links-to-external-tools]] +== Links To External Tools + +Gerrit has extension points that enables development of a +light-weight plugin that links commits to external +tools (GitBlit, CGit, company specific resources etc). + +PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI. + +[source, java] +---- +import com.google.gerrit.extensions.annotations.Listen; +import com.google.gerrit.extensions.webui.PatchSetWebLink;; + +@Listen +public class MyWeblinkPlugin implements PatchSetWebLink { + + private String name = "MyLink"; + private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s"; + + @Override + public String getLinkName() { + return name ; + } + + @Override + public String getPatchSetUrl(String project, String commit) { + return String.format(placeHolderUrlProjectCommit, project, commit); + } + +} +---- +  [[documentation]]  == Documentation   
diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt index cd22d70..5a65cfb 100644 --- a/Documentation/rest-api-changes.txt +++ b/Documentation/rest-api-changes.txt 
@@ -242,6 +242,11 @@  authenticated and has commented on the current revision.  --   +[[patch-set-links]] +-- +* `PATCHSET_LINKS`: include the webLinks field. +-- +  .Request  ----  GET /changes/?q=97&o=CURRENT_REVISION&o=CURRENT_COMMIT&o=CURRENT_FILES&o=DOWNLOAD_COMMANDS HTTP/1.0 @@ -3243,6 +3248,8 @@  Actions the caller might be able to perform on this revision. The  information is a map of view name to link:#action-info[ActionInfo]  entities. +|'webLinks' |optional| +Links to patch set in external tools as a list.  |===========================    [[rule-input]] 
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ListChangesOption.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ListChangesOption.java index c23e312..f9f8b62 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ListChangesOption.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ListChangesOption.java 
@@ -49,7 +49,10 @@  DRAFT_COMMENTS(12),    /** Include download commands for the caller. */ - DOWNLOAD_COMMANDS(13); + DOWNLOAD_COMMANDS(13), + + /** Include patch set weblinks. */ + WEB_LINKS(14);    private final int value;   
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/RevisionInfo.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/RevisionInfo.java index ea5b068..7ad63b4 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/RevisionInfo.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/RevisionInfo.java 
@@ -14,6 +14,7 @@    package com.google.gerrit.extensions.common;   +import java.util.List;  import java.util.Map;    public class RevisionInfo { @@ -25,4 +26,15 @@  public CommitInfo commit;  public Map<String, FileInfo> files;  public Map<String, ActionInfo> actions; + public List<WebLinkInfo> webLinks; + + public static class WebLinkInfo { + public String linkName; + public String linkUrl; + + public WebLinkInfo(String name, String url) { + linkName = name; + linkUrl = url; + } + }  } 
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/PatchSetWebLink.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/PatchSetWebLink.java new file mode 100644 index 0000000..b6086f2 --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/PatchSetWebLink.java 
@@ -0,0 +1,29 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.gerrit.extensions.webui; + +import com.google.gerrit.extensions.annotations.ExtensionPoint; + +@ExtensionPoint +public interface PatchSetWebLink extends WebLink { + + /** + * URL to patch set in external service. + * + * @param projectName Name of the project + * @param commit Commit of the patch set + * @return url to patch set in external service. + */ + String getPatchSetUrl(final String projectName, final String commit); +} 
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/WebLink.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/WebLink.java new file mode 100644 index 0000000..3b06aa7 --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/WebLink.java 
@@ -0,0 +1,26 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.gerrit.extensions.webui; + +import com.google.gerrit.extensions.annotations.ExtensionPoint; + +public interface WebLink { + + /** + * The link-name displayed in UI. + * + * @return name of link. + */ + String getLinkName(); +} 
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java index 9b9c092..0fa238b 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java 
@@ -202,7 +202,8 @@  RestApi call = ChangeApi.detail(changeId.get());  ChangeList.addOptions(call, EnumSet.of(  ListChangesOption.CURRENT_ACTIONS, - ListChangesOption.ALL_REVISIONS)); + ListChangesOption.ALL_REVISIONS, + ListChangesOption.WEB_LINKS));  if (!fg) {  call.background();  } 
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.java index 931a94b..e0af9c4 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.java 
@@ -21,24 +21,27 @@  import com.google.gerrit.client.changes.ChangeInfo.CommitInfo;  import com.google.gerrit.client.changes.ChangeInfo.GitPerson;  import com.google.gerrit.client.changes.ChangeInfo.RevisionInfo; +import com.google.gerrit.client.changes.ChangeInfo.WebLinkInfo; +import com.google.gerrit.client.rpc.Natives;  import com.google.gerrit.client.ui.CommentLinkProcessor;  import com.google.gerrit.client.ui.InlineHyperlink;  import com.google.gerrit.common.PageLinks;  import com.google.gerrit.reviewdb.client.Change.Status;  import com.google.gwt.core.client.GWT; -import com.google.gwt.dom.client.AnchorElement; +import com.google.gwt.core.client.JsArray;  import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.TableCellElement;  import com.google.gwt.event.dom.client.ClickEvent;  import com.google.gwt.resources.client.CssResource;  import com.google.gwt.uibinder.client.UiBinder;  import com.google.gwt.uibinder.client.UiField;  import com.google.gwt.uibinder.client.UiHandler; +import com.google.gwt.user.client.ui.Anchor;  import com.google.gwt.user.client.ui.Button;  import com.google.gwt.user.client.ui.Composite;  import com.google.gwt.user.client.ui.HTML;  import com.google.gwt.user.client.ui.HTMLPanel;  import com.google.gwt.user.client.ui.ScrollPanel; -import com.google.gwt.user.client.ui.UIObject;  import com.google.gwtexpui.clippy.client.CopyableLabel;  import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;   @@ -53,7 +56,7 @@    @UiField Style style;  @UiField CopyableLabel commitName; - @UiField AnchorElement browserLink; + @UiField TableCellElement webLinkCell;  @UiField InlineHyperlink authorNameEmail;  @UiField Element authorDate;  @UiField InlineHyperlink committerNameEmail; @@ -100,14 +103,31 @@  committerDate, change.status());  text.setHTML(commentLinkProcessor.apply(  new SafeHtmlBuilder().append(commit.message()).linkify())); + setWebLinks(change, revision, revInfo); + }   + private void setWebLinks(ChangeInfo change, String revision, + RevisionInfo revInfo) {  GitwebLink gw = Gerrit.getGitwebLink();  if (gw != null && gw.canLink(revInfo)) { - browserLink.setInnerText(gw.getLinkName()); - browserLink.setHref(gw.toRevision(change.project(), revision)); - } else { - UIObject.setVisible(browserLink, false); + addWebLink(gw.toRevision(change.project(), revision), gw.getLinkName());  } + + JsArray<WebLinkInfo> links = revInfo.web_links(); + + if (links != null) { + for (WebLinkInfo link : Natives.asList(links)) { + addWebLink(link.link_url(), link.link_name()); + } + } + } + + private void addWebLink(String href, String name) { + Anchor a = new Anchor(); + a.setHref(href); + a.setText(parenthesize(name)); + Element el = a.getElement(); + webLinkCell.appendChild(el);  }    private static void formatLink(GitPerson person, InlineHyperlink name, @@ -118,6 +138,14 @@  date.setInnerText(FormatUtil.mediumFormat(person.date()));  }   + private static String parenthesize(String str) { + return new StringBuilder() + .append("(") + .append(str) + .append(")") + .toString(); + } +  private static String renderName(GitPerson person) {  return person.name() + " <" + person.email() + ">";  } 
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.ui.xml b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.ui.xml index c02e9f8..7ca66f9 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.ui.xml +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/CommitBox.ui.xml 
@@ -79,6 +79,10 @@  top: 0px;  right: -16px;  } + <!-- To make room for the copyableLabel from the adjacent column --> + .webLinkCell a:first-child { + margin-left:16px; + }  </ui:style>  <g:HTMLPanel>  <g:ScrollPanel styleName='{style.scroll}' ui:field='scroll'> @@ -114,7 +118,7 @@  <tr>  <th><ui:msg>Commit</ui:msg></th>  <td><clippy:CopyableLabel styleName='{style.clippy}' ui:field='commitName'/></td> - <td><a style="margin-left:16px;" ui:field='browserLink' href=""/></td> + <td ui:field='webLinkCell' class='{style.webLinkCell}'></td>  </tr>  <tr>  <th><ui:msg>Change-Id</ui:msg></th> 
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeInfo.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeInfo.java index bfe70b8..a2e8748 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeInfo.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeInfo.java 
@@ -218,6 +218,7 @@    public final native boolean has_fetch() /*-{ return this.hasOwnProperty('fetch') }-*/;  public final native NativeMap<FetchInfo> fetch() /*-{ return this.fetch; }-*/; + public final native JsArray<WebLinkInfo> web_links() /*-{ return this.web_links; }-*/;    public static void sortRevisionInfoByNumber(JsArray<RevisionInfo> list) {  Collections.sort(Natives.asList(list), new Comparator<RevisionInfo>() { @@ -296,4 +297,11 @@  protected IncludedInInfo() {  }  } + + public static class WebLinkInfo extends JavaScriptObject { + public final native String link_name() /*-{ return this.link_name; }-*/; + public final native String link_url() /*-{ return this.link_url; }-*/; + protected WebLinkInfo() { + } + }  } 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/WebLinks.java b/gerrit-server/src/main/java/com/google/gerrit/server/WebLinks.java new file mode 100644 index 0000000..d2bc8b7 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/WebLinks.java 
@@ -0,0 +1,51 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.gerrit.server; + +import com.google.common.collect.Lists; +import com.google.gerrit.extensions.registration.DynamicSet; +import com.google.gerrit.extensions.webui.PatchSetWebLink; +import com.google.inject.Inject; + +import java.util.List; + +public class WebLinks { + + private DynamicSet<PatchSetWebLink> patchSetLinks; + + @Inject + public WebLinks(final DynamicSet<PatchSetWebLink> patchSetLinks) { + this.patchSetLinks = patchSetLinks; + } + + public Iterable<Link> getPatchSetLinks(final String project, + final String commit) { + List<Link> links = Lists.newArrayList(); + for (PatchSetWebLink webLink : patchSetLinks) { + links.add(new Link(webLink.getLinkName(), + webLink.getPatchSetUrl(project, commit))); + } + return links; + } + + public class Link { + public String name; + public String url; + + public Link(String name, String url) { + this.name = name; + this.url = url; + } + } +} 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/WebLinksProvider.java b/gerrit-server/src/main/java/com/google/gerrit/server/WebLinksProvider.java new file mode 100644 index 0000000..71c13a3 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/WebLinksProvider.java 
@@ -0,0 +1,35 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.gerrit.server; + +import com.google.gerrit.extensions.registration.DynamicSet; +import com.google.gerrit.extensions.webui.PatchSetWebLink; +import com.google.inject.Inject; +import com.google.inject.Provider; + +public class WebLinksProvider implements Provider<WebLinks> { + + private DynamicSet<PatchSetWebLink> patchSetLinks; + + @Inject + public WebLinksProvider(DynamicSet<PatchSetWebLink> patchSetLinks) { + this.patchSetLinks = patchSetLinks; + } + + @Override + public WebLinks get() { + WebLinks webLinks = new WebLinks(patchSetLinks); + return webLinks; + } +} 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeJson.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeJson.java index ad6b804..4a7cbdb 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeJson.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeJson.java 
@@ -28,6 +28,7 @@  import static com.google.gerrit.extensions.common.ListChangesOption.LABELS;  import static com.google.gerrit.extensions.common.ListChangesOption.MESSAGES;  import static com.google.gerrit.extensions.common.ListChangesOption.REVIEWED; +import static com.google.gerrit.extensions.common.ListChangesOption.WEB_LINKS;    import com.google.common.base.Joiner;  import com.google.common.base.Objects; @@ -79,6 +80,7 @@  import com.google.gerrit.server.AnonymousUser;  import com.google.gerrit.server.CurrentUser;  import com.google.gerrit.server.IdentifiedUser; +import com.google.gerrit.server.WebLinks;  import com.google.gerrit.server.account.AccountInfo;  import com.google.gerrit.server.extensions.webui.UiActions;  import com.google.gerrit.server.git.LabelNormalizer; @@ -152,6 +154,7 @@  private ChangeControl lastControl;  private Set<Change.Id> reviewed;  private LoadingCache<Project.NameKey, ProjectControl> projectControls; + private Provider<WebLinks> webLinks;    @Inject  ChangeJson( @@ -169,7 +172,8 @@  DynamicMap<DownloadScheme> downloadSchemes,  DynamicMap<DownloadCommand> downloadCommands,  DynamicMap<RestView<ChangeResource>> changeViews, - Revisions revisions) { + Revisions revisions, + Provider<WebLinks> webLinks) {  this.db = db;  this.labelNormalizer = ln;  this.userProvider = user; @@ -185,6 +189,7 @@  this.downloadCommands = downloadCommands;  this.changeViews = changeViews;  this.revisions = revisions; + this.webLinks = webLinks;    options = EnumSet.noneOf(ListChangesOption.class);  projectControls = CacheBuilder.newBuilder() @@ -328,7 +333,7 @@  if (has(ALL_REVISIONS)  || has(CURRENT_REVISION)  || limitToPsId.isPresent()) { - out.revisions = revisions(cd, limitToPsId); + out.revisions = revisions(cd, limitToPsId, out.project);  if (out.revisions != null) {  for (Map.Entry<String, RevisionInfo> entry : out.revisions.entrySet()) {  if (entry.getValue().isCurrent) { @@ -790,7 +795,7 @@  }    private Map<String, RevisionInfo> revisions(ChangeData cd, - Optional<PatchSet.Id> limitToPsId) throws OrmException { + Optional<PatchSet.Id> limitToPsId, String project) throws OrmException {  ChangeControl ctl = control(cd);  if (ctl == null) {  return null; @@ -819,13 +824,13 @@  Map<String, RevisionInfo> res = Maps.newLinkedHashMap();  for (PatchSet in : src) {  if (ctl.isPatchVisible(in, db.get())) { - res.put(in.getRevision().get(), toRevisionInfo(cd, in)); + res.put(in.getRevision().get(), toRevisionInfo(cd, in, project));  }  }  return res;  }   - private RevisionInfo toRevisionInfo(ChangeData cd, PatchSet in) + private RevisionInfo toRevisionInfo(ChangeData cd, PatchSet in, String project)  throws OrmException {  RevisionInfo out = new RevisionInfo();  out.isCurrent = in.getId().equals(cd.change().currentPatchSetId()); @@ -873,6 +878,13 @@  : null;  }   + if (has(WEB_LINKS)) { + out.webLinks = Lists.newArrayList(); + for (WebLinks.Link link : webLinks.get().getPatchSetLinks( + project, in.getRevision().get())) { + out.webLinks.add(new RevisionInfo.WebLinkInfo(link.name, link.url)); + } + }  return out;  }   @@ -885,7 +897,6 @@  commit.committer = toGitPerson(info.getCommitter());  commit.subject = info.getSubject();  commit.message = info.getMessage(); -  for (ParentInfo parent : info.getParents()) {  CommitInfo i = new CommitInfo();  i.commit = parent.id.get(); 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java index c6598d4..f09cd7c 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java 
@@ -31,6 +31,7 @@  import com.google.gerrit.extensions.registration.DynamicMap;  import com.google.gerrit.extensions.registration.DynamicSet;  import com.google.gerrit.extensions.systemstatus.MessageOfTheDay; +import com.google.gerrit.extensions.webui.PatchSetWebLink;  import com.google.gerrit.extensions.webui.TopMenu;  import com.google.gerrit.reviewdb.client.AccountGroup;  import com.google.gerrit.rules.PrologModule; @@ -42,6 +43,8 @@  import com.google.gerrit.server.IdentifiedUser;  import com.google.gerrit.server.MimeUtilFileTypeRegistry;  import com.google.gerrit.server.PluginUser; +import com.google.gerrit.server.WebLinks; +import com.google.gerrit.server.WebLinksProvider;  import com.google.gerrit.server.account.AccountByEmailCacheImpl;  import com.google.gerrit.server.account.AccountCacheImpl;  import com.google.gerrit.server.account.AccountControl; @@ -224,6 +227,7 @@  .in(SINGLETON);  bind(FromAddressGenerator.class).toProvider(  FromAddressGeneratorProvider.class).in(SINGLETON); + bind(WebLinks.class).toProvider(WebLinksProvider.class).in(SINGLETON);    bind(PatchSetInfoFactory.class);  bind(IdentifiedUser.GenericFactory.class).in(SINGLETON); @@ -266,6 +270,7 @@  DynamicMap.mapOf(binder(), DownloadScheme.class);  DynamicMap.mapOf(binder(), DownloadCommand.class);  DynamicMap.mapOf(binder(), ProjectConfigEntry.class); + DynamicSet.setOf(binder(), PatchSetWebLink.class);    bind(AnonymousUser.class);